/*
 * Copyright 2012 Gregor Schauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.schauer.gregor.vienna.datastore;

import at.schauer.gregor.vienna.context.ViennaContext;
import at.schauer.gregor.vienna.metadata.MetadataProvider;

/**
 * @author Gregor Schauer
 */
public class StorageMetadata<T> {
	public T object;
	public Class<T> type;
	public String entityName;
	public String identifierName;
	public Object identifier;

	@SuppressWarnings("unchecked")
	public static <T> StorageMetadata<T> create(ViennaContext ctx, T obj) {
		Class<T> type = (Class<T>) obj.getClass();
		MetadataProvider<T> metadataProvider = ctx.getMetadataProvider(type);
		String entityName = metadataProvider.getEntityName(type);
		String identifierName = metadataProvider.getIdentifierName(type);
		Object identifier = metadataProvider.getIdentifier(obj);
		return new StorageMetadata<T>(obj, type, entityName, identifierName, identifier);
	}

	public static <T> StorageMetadata<T> create(ViennaContext ctx, Class<T> type) {
		MetadataProvider<T> metadataProvider = ctx.getMetadataProvider(type);
		String entityName = metadataProvider.getEntityName(type);
		String identifierName = metadataProvider.getIdentifierName(type);
		return new StorageMetadata<T>(null, type, entityName, identifierName, null);
	}

	StorageMetadata(T object, Class<T> type, String entityName, String identifierName, Object identifier) {
		this.object = object;
		this.type = type;
		this.entityName = entityName;
		this.identifierName = identifierName;
		this.identifier = identifier;
	}
}
